package com.ibt.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ibt.dao.ModuleDAO;
import com.ibt.dao.ProjectEntryDAO;
import com.ibt.entity.Business;
import com.ibt.entity.Login;
import com.ibt.entity.Module;
import com.ibt.entity.ProjectEntry;

@Controller
@RequestMapping("/newproject")
public class ProjectController {
	@RequestMapping(value="/add.do",method = RequestMethod.POST)
	public ModelAndView userEntry(@RequestParam("pname") String pname, 
			@RequestParam("pver") String pver, 
			@RequestParam("pmanager") String pmanager,
			@RequestParam("pdesc") String pdesc, 
			@RequestParam("edate") String pend,
			@RequestParam("sdate") String pstart, 
			@RequestParam("nom") String nom,
			//@RequestParam("noemp") String noemployees,
			 
			ModelAndView model,HttpSession session,HttpServletRequest req,HttpServletResponse res) throws Exception {
		String rcount=req.getParameter("rowcount");
		int rowcount=Integer.parseInt(rcount);
		System.out.println("row count "+rowcount);
		Business bus=(Business)session.getAttribute("business");
		Login  Ulogin=(Login)session.getAttribute("user");
		int busId=bus.getBusinessId();
		
		ProjectEntry pEntry=new ProjectEntry(bus,pname,pver,pmanager,pdesc,new Date(),new Date(),nom);
		  try {
				ProjectEntryDAO.insert(pEntry);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		for(int i=1;i<=rowcount;i++){
			String module=req.getParameter("module"+i);
			String desc=req.getParameter("desc"+i);
			String projname=req.getParameter("other"+i);
			Module mod=null;
			try {
				mod=new Module(pEntry,bus,module,desc,pname);
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
			ModuleDAO.insert(mod);
		}
   
		System.out.println("in x0029 searching  BusinessUserid " );
		model.setViewName("/jsp/addproject.jsp");
		model.addObject("msg","New Project is Added to The System with ProjectName:"+pname);
		return model;

}
	

}
